package busca.tesoros.uclm;


import android.app.Activity;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;



import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ClienteBuscaTesorosActivity extends Activity implements OnClickListener  {
	
	public static final String SERVERIP = "161.67.0.20";
	public static final int SERVERPORT = 8080;
	
	private Button login;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        View ButtonContinue = findViewById(R.id.loginOk);
        ButtonContinue.setOnClickListener(this); 
    } 
    
    


	protected void onDestroy() {
    	super.onDestroy();
    }
    
    
    private OnClickListener comprobarUsuario = new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		
    	
        		final EditText usuario = (EditText) findViewById(R.id.loginUser);
        		final EditText pass = (EditText) findViewById(R.id.loginPassword);
        		
        		
               final String user =usuario.getText().toString();
               final String password= pass.getText().toString();
         
   
        	// Use an AsyncTask to avoid blocking the UI thread
        	new AsyncTask<Void, Void, String>() {
        		
        		@Override
        		protected String doInBackground(Void... arg0) {
        		    return serverDialog("{ usuario:" + user + "pass:" +password +"}");
        		}
        		@Override
        		protected void onPostExecute(String inTxt) {
        		    try
        		    {
        		    		JSONObject inJson = new JSONObject(inTxt);
        		    		String response= inJson.getString("response");
        		    		
        		    		if(response.equals("true"))
        		    		  mostrarResultado(true);

        		    		else
        		    			mostrarResultado(false);
        		    			
       		    
        		    }
        		    catch (Exception ex)
        		    {
        		    	Log.e("Client","JSON Error: " + ex.getMessage());
        		    }        			
        		}
        	}.execute();	
        	
        	
    	}
    };
    
    private void mostrarResultado (boolean resultado){
    	if (resultado == true){
    		Toast brindis = Toast.makeText(this, "Login correcto", 3000);
  		  brindis.setGravity (Gravity.TOP, -30, 50); 
  		  brindis.show ();
    	    	}
    	else{
  		  Toast brindis = Toast.makeText (this, "Login Incorrecto", 3000); 
  		  brindis.setGravity (Gravity.TOP, -30, 50); 
  		  brindis.show ();		
    	}
    		
    }
        	
        
            private String serverDialog(String outTxt)
            {
        		String inTxt = "";
        		try {
        			Log.i("Client", "Conectting to "+ SERVERIP + ":" + SERVERPORT);
        			InetAddress serverAddr = InetAddress.getByName(SERVERIP);
            		Socket clientSocket = new Socket(serverAddr, SERVERPORT);
        			
        			if(clientSocket.isConnected()) {
        				
        				PrintWriter out = new PrintWriter(
        					new BufferedWriter(new OutputStreamWriter(clientSocket
        						.getOutputStream())), true);

        				out.println(outTxt);

        				BufferedReader in = new BufferedReader(
        						new InputStreamReader(clientSocket.getInputStream()),1024);

        				        			
        				Log.i("Client", "Response from server:\n");
        				String line;
        				while ((line = in.readLine()) != null) {
        					Log.i("Client", line);
        					inTxt += line;
        				}
        				Log.i("Client", "-----------------\n");
        			}
        			clientSocket.close();
        		}
        		catch (Exception e) {
        			Log.e("Client", "Error connecting to the library server.", e);
        		}
        		return inTxt;
        	}




			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}

    
        	}
    
    
	
